<?php
namespace Api\Controller;
use Think\Controller;

class SchooloutController extends BaseController {

   private $m = NULL;
   private $md = NULL;
   private $navName = '订单管理';


   public function _initialize(){
   		parent::_initialize();
		$this->m = M('shop');
		$this->md = M('shop_detail');
		
		
   }
    	 	
	/** 0619  校边外卖-猜你喜欢  liuq
	*
	*/	
  
   public  function slist(){
       $page = I('page');
       //$cid = I('cid');//  用户标识  member id
       $data = $this->m->where('shop_level_two = 18 AND buss_status = 2')->order('id desc')->page($page.',10')->select();      
       $count_num  = $this->m->where('shop_level_two = 18 AND buss_status = 2')->count();
       foreach( $data as $key => $item){
           $data[$key]['min_delivery_fee'] = $this->md->where('s_id="'.$item['id'].'"')->getField('min_delivery_fee');//起送费
             $data[$key]['delivery_fee'] = $this->md->where('s_id="'.$item['id'].'"')->getField('delivery_fee');//起送费
               $data[$key]['is_rest'] = $this->md->where('s_id="'.$item['id'].'"')->getField('is_rest');// 1 正常营业  2休息
                 $data[$key]['class_name'] = M('shop_product_class')->where('id="'.$item['shop_level_three'].'"')->getField('class_name'); //http://localhost:81/jdrapia/Uploads/1.jpg
                   $data[$key]['shop_heapic'] = $this->md->where("s_id=".$item['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.$this->md->where("s_id=".$item['id'])->getField('shop_heapic'):""; //店铺头像
                 
                   $attitude = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('attitude_star'); 
                   $speed = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('speed_star'); 
                   $taste = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('taste_star'); 
                   $evaluate_times = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->count();
              
                   $data[$key]['comment_star'] = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));
                 
                   if( $data[$key]['comment_star'] == 1 ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                   }elseif(  $data[$key]['comment_star'] == 2  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                   }elseif(  $data[$key]['comment_star'] == 3  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                   }elseif(  $data[$key]['comment_star'] == 4  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                   }elseif(  $data[$key]['comment_star'] == 5  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                   }else{
                       $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                   }
                 
                 }

     // returnApiData2('0','成功',$data, $data2,$data3);exit;	 
     returnApiData3('0','成功',$data, $data2,$data3,$count_num);exit;	     
   }
   
       /** 0619  校边外卖-猜你喜欢_顶部8个固定选项 liuq
	*
	*/	
     public  function slist_top_eight(){
         $page = I('page');
         //$cid = I('cid');//  用户标识  member id
         $class_id = I('class_id');// 商家类型  第四级  写死8个   米饭20  面食21  西式简餐 22   蛋糕甜品23   风味小吃24   粥25   甜品饮品26  品质单炒27
         $data = $this->m->where('shop_level_two = 18  AND buss_status = 2 AND shop_level_three = "'.$class_id.'" ')->order('id desc')->page($page.',10')->select();   
         $count_num  = $this->m->where('shop_level_two = 18  AND buss_status = 2 AND shop_level_three = "'.$class_id.'" ')->count();
         foreach( $data as $key => $item){
             $data[$key]['min_delivery_fee'] = $this->md->where('s_id="'.$item['id'].'"')->getField('min_delivery_fee');//起送费
               $data[$key]['delivery_fee'] = $this->md->where('s_id="'.$item['id'].'"')->getField('delivery_fee');//起送费
                 $data[$key]['class_name'] = M('shop_product_class')->where('id="'.$item['shop_level_three'].'"')->getField('class_name'); //http://localhost:81/jdrapia/Uploads/1.jpg
                   $data[$key]['shop_heapic'] = $this->md->where("s_id=".$item['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.$this->md->where("s_id=".$item['id'])->getField('shop_heapic'):""; //店铺头像
                    
                   $attitude = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('attitude_star'); 
                   $speed = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('speed_star'); 
                   $taste = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('taste_star'); 
                   $evaluate_times = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->count();
              
                   $data[$key]['comment_star'] = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));
                 
                   if( $data[$key]['comment_star'] == 1 ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                   }elseif(  $data[$key]['comment_star'] == 2  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                   }elseif(  $data[$key]['comment_star'] == 3  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                   }elseif(  $data[$key]['comment_star'] == 4  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                   }elseif(  $data[$key]['comment_star'] == 5  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                   }else{
                       $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                   }
                   
                 }
                 
        // returnApiData2('0','成功',$data, $data2,$data3);exit;	 
        returnApiData3('0','成功',$data, $data2,$data3,$count_num);exit;	     
     }
   
     
      public  function eight_order(){
         $page  = I('page');
         $class_id = I('class_id');// 商家类型  第三级
         $order_type  =  I('order_type'); //排序条件   1距离最近   2 销量最高  3 起送价最低  4 配送速度最快 5 人气最旺
         if( $order_type == 3 ){
              $data =  $this->m->where('shop_level_two = 18 AND  buss_status = 2 AND  shop_level_three="'. $class_id .'"')->order('id  desc')->page($page.',10')->select();
              $count_num  = $this->m->where('shop_level_two = 18 AND  buss_status = 2 AND  shop_level_three="'. $class_id .'"')->count();
               foreach ( $data as $key => $item ){
                 $data[$key]['min_delivery_fee'] = $this->md->where('s_id="'.$item['id'].'"')->getField('min_delivery_fee');
                  $data[$key]['delivery_fee'] = $this->md->where('s_id="'.$item['id'].'"')->getField('delivery_fee');
                  ////
                    $data[$key]['class_name'] = M('shop_product_class')->where('id="'.$item['shop_level_three'].'"')->getField('class_name'); //http://localhost:81/jdrapia/Uploads/1.jpg
                      $data[$key]['shop_heapic'] = $this->md->where("s_id=".$item['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.$this->md->where("s_id=".$item['id'])->getField('shop_heapic'):""; //店铺头像
                 
                         
                   $attitude = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('attitude_star'); 
                   $speed = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('speed_star'); 
                   $taste = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('taste_star'); 
                   $evaluate_times = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->count();
              
                   $data[$key]['comment_star'] = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));
                 
                            if( $data[$key]['comment_star'] == 1 ){
                               $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                            }elseif(  $data[$key]['comment_star'] == 2  ){
                               $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                            }elseif(  $data[$key]['comment_star'] == 3  ){
                               $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                            }elseif(  $data[$key]['comment_star'] == 4  ){
                               $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                            }elseif(  $data[$key]['comment_star'] == 5  ){
                               $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                            }else{
                                $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                            }
                  
               }
                //$flag = array();  
          
      }
      
      
      if( $order_type == 2  ||  $order_type == 5  ){
               $data = M('shop_product_order')->field("count(*) as count,from_mid")->group("from_mid")->select();
                foreach( $data as $key => $item ){
                   $data[$key]['shop_id'] = $this->m->where('mid="'.$item['from_mid'].'" AND shop_level_two = 18 AND  buss_status = 2 AND  shop_level_three="'. $class_id .'"')->getField('id');
                     $data[$key]['class_name'] = $this->m->where('id="'.$data[$key]['shop_id'].'"')->getField('class_name');
                       $data[$key]['min_delivery_fee'] = $this->md->where('s_id="'.$data[$key]['shop_id'].'"')->getField('min_delivery_fee');
                         $data[$key]['delivery_fee'] = $this->md->where('s_id="'.$data[$key]['shop_id'].'"')->getField('delivery_fee');
                         
                             
                   $attitude = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('attitude_star'); 
                   $speed = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('speed_star'); 
                   $taste = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('taste_star'); 
                   $evaluate_times = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->count();
              
                   $data[$key]['comment_star'] = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));
                 
                        if( $data[$key]['comment_star'] == 1 ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                        }elseif(  $data[$key]['comment_star'] == 2  ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                        }elseif(  $data[$key]['comment_star'] == 3  ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                        }elseif(  $data[$key]['comment_star'] == 4  ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                        }elseif(  $data[$key]['comment_star'] == 5  ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                        }else{
                            $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                        }
                         
                   }
                
         }
           
          
          foreach($data as $v){  
             $flag[] = $v['min_delivery_fee'];  
          }  
          array_multisort($flag, SORT_ASC, $data); 
         // returnApiData2('0','成功',$data, $data2,$data3);exit;	   
          returnApiData3('0','成功',$data, $data2,$data3,$count_num);exit;	    
    }
     
    
     
       public  function eight_order_01(){
       
         $class_id = I('class_id');// 商家类型  第三级
         $order_type  =  I('order_type'); //排序条件   1距离最近   2 销量最高  3 起送价最低  4 配送速度最快 5 人气最旺
        
         
         $class_id = 20;// 商家类型  第三级
         $order_type  =  3; //排序条件   1距离最近   2 销量最高  3 起送价最低  4 配送速度最快 5 人气最旺
        
         if( $order_type == 3 ){
            $data =  $this->md->where()->order('min_delivery_fee  desc')->select();
            foreach ( $data as $key => $item ){
                 $data[$key]['shop_id'] = $this->m->where('id="'.$item['s_id'].'" AND shop_level_two = 18 AND  buss_status = 2 AND  shop_level_three="'. $class_id .'" ')->getField('id');
                  $data[$key]['class_name'] = $this->m->where('id="'.$data[$key]['shop_id'].'"')->getField('class_name');
                   $data[$key]['min_delivery_fee'] = $this->md->where('s_id="'.$data[$key]['shop_id'].'"')->getField('min_delivery_fee');
                     $data[$key]['delivery_fee'] = $this->md->where('s_id="'.$data[$key]['shop_id'].'"')->getField('delivery_fee');
                      
                         
                   $attitude = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('attitude_star'); 
                   $speed = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('speed_star'); 
                   $taste = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('taste_star'); 
                   $evaluate_times = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->count();
              
                   $data[$key]['comment_star'] = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));
                 
                   if( $data[$key]['comment_star'] == 1 ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                   }elseif(  $data[$key]['comment_star'] == 2  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                   }elseif(  $data[$key]['comment_star'] == 3  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                   }elseif(  $data[$key]['comment_star'] == 4  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                   }elseif(  $data[$key]['comment_star'] == 5  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                   }else{
                       $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                   }
                     
            }
            
          if( $order_type == 2  ||  $order_type == 5  ){
               $data = M('shop_product_order')->field("count(*) as count,from_mid")->group("from_mid")->select();
                foreach( $data as $key => $item ){
                   $data[$key]['shop_id'] = $this->m->where('mid="'.$item['from_mid'].'" AND shop_level_two = 3 AND  buss_status = 2 AND  shop_level_three="'. $class_id .'"')->getField('id');
                     $data[$key]['class_name'] = $this->m->where('id="'.$data[$key]['shop_id'].'"')->getField('class_name');
                       $data[$key]['min_delivery_fee'] = $this->md->where('s_id="'.$data[$key]['shop_id'].'"')->getField('min_delivery_fee');
                         $data[$key]['delivery_fee'] = $this->md->where('s_id="'.$data[$key]['shop_id'].'"')->getField('delivery_fee');
                         
                             
                   $attitude = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('attitude_star'); 
                   $speed = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('speed_star'); 
                   $taste = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->sum('taste_star'); 
                   $evaluate_times = M('shop_evaluate')->where('mid="'.$item['mid'].'"')->count();
              
                   $data[$key]['comment_star'] = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));
                 
                        if( $data[$key]['comment_star'] == 1 ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                        }elseif(  $data[$key]['comment_star'] == 2  ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                        }elseif(  $data[$key]['comment_star'] == 3  ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                        }elseif(  $data[$key]['comment_star'] == 4  ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                        }elseif(  $data[$key]['comment_star'] == 5  ){
                           $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                        }else{
                            $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                        }
                         
                   }
                
            }
              
                        
         }
         
         returnApiData2('0','成功',$data, $data2,$data3);exit;	
      
     }
     
     
     public function choice(){
 
         
         $page = I('page'); 
         $class_id = I('class_id');// 商家类型  顶部8个固定选项      写死8个   米饭20  面食21  西式简餐 22   蛋糕甜品23   风味小吃24   粥25   甜品饮品26  品质单炒27
         $out_money_type = I('out_money_type'); //1,
       
         if(!empty($out_money_type)){
             $where['out_money_type'] = $out_money_type;
         }
         
  
         $data = $this->md->where($where)->order('id desc')->page($page.',10')->select();
         $count_num = $this->md->where($where)->order('add_time desc')->count();
 
         foreach ($data as $key => $value) {
           
            $shop_level_three=M('shop')->where("id=".$value['s_id'])->getField('shop_level_three');
            $shop=M('shop')->where("id=".$value['s_id'])->find();

            //$shop_level_three=M('shop')->where("id=".$value['s_id'])->find();
            //$shop=M('shop')->where("id=".$value['s_id'])->find();

           if($class_id==20){
                if($shop_level_three==20){
                  $string.=$shop['id'].'|';
           }
           }
           if($class_id==21){
                if($shop_level_three==21){
                  $string.=$shop['id'].'|';
           }
           }
           if($class_id==22){
                if($shop_level_three==22){
                  $string.=$shop['id'].'|';
           }
           }
           if($class_id==23){
                if($shop_level_three==23){
                  $string.=$shop['id'].'|';
           }
           }
           if($class_id==24){
                if($shop_level_three==24){
                  $string.=$shop['id'].'|';
           }
           }
           if($class_id==25){
                if($shop_level_three==25){
                  $string.=$shop['id'].'|';
           }
           }
           if($class_id==26){
                if($shop_level_three==26){
                  $string.=$shop['id'].'|';
           }
           }
           if($class_id==27){
                if($shop_level_three==27){
                  $string.=$shop['id'].'|';
           }
           }
           
         }
          $array=explode("|", $string);
          array_pop($array);
          foreach ($array as $key => $value) {
              
              
//              
//           $data4[$key]['shop_name'] = M('shop')->where('id="'.$value.'"')->getField('shop_name');
//              $data4[$key]['per_consumption'] = M('shop_detail')->where('s_id="'.$value.'"')->getField('per_consumption');
//                $data4[$key]['start_time'] = M('shop_detail')->where('s_id="'.$value.'"')->getField('start_time'); 
//                    $data4[$key]['stop_time'] = M('shop_detail')->where('s_id="'.$value.'"')->getField('stop_time'); 
//                      $data4[$key]['address'] = M('shop_detail')->where('s_id="'.$value.'"')->getField('address'); 
//                         $data4[$key]['shop_heapic'] = $this->md->where('s_id="'.$value.'"')->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.$this->md->where("s_id=".$item['id'])->getField('shop_heapic'):""; //店铺头像
//  
//         
                        $data4[$key]['mid'] = $this->m->where('id="'.$value.'"')->getField('mid');
                           $data4[$key]['shop_name'] = $this->m->where('id="'.$value.'"')->getField('shop_name');
                           $data4[$key]['min_delivery_fee'] = $this->md->where('s_id="'.$value.'"')->getField('min_delivery_fee');//起送费
                             $data4[$key]['delivery_fee'] = $this->md->where('s_id="'.$value.'"')->getField('delivery_fee');//起送费
                               $shop_level_three_id = $this->m->where('id="'.$value.'"')->getField('shop_level_three');
                                $data4[$key]['class_name'] = M('shop_product_class')->where('id="'.$shop_level_three_id.'"')->getField('class_name'); //http://localhost:81/jdrapia/Uploads/1.jpg
                                  $data4[$key]['shop_heapic'] = $this->md->where("s_id=".$value)->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.$this->md->where("s_id=".$item['id'])->getField('shop_heapic'):""; //店铺头像

                                  $attitude = M('shop_evaluate')->where('mid="'.$data4[$key]['mid'].'"')->sum('attitude_star'); 
                                  $speed = M('shop_evaluate')->where('mid="'.$data4[$key]['mid'].'"')->sum('speed_star'); 
                                  $taste = M('shop_evaluate')->where('mid="'.$data4[$key]['mid'].'"')->sum('taste_star'); 
                                  $evaluate_times = M('shop_evaluate')->where('mid="'.$data4[$key]['mid'].'"')->count();

                                  $data4[$key]['comment_star'] = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));

                                  if( $data4[$key]['comment_star'] == 1 ){
                                     $data4[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                                  }elseif(  $data[$key]['comment_star'] == 2  ){
                                     $data4[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                                  }elseif(  $data[$key]['comment_star'] == 3  ){
                                     $data4[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                                  }elseif(  $data[$key]['comment_star'] == 4  ){
                                     $data4[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                                  }elseif(  $data[$key]['comment_star'] == 5  ){
                                     $data4[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                                  }else{
                                     $data4[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                                 }           

                         
               }
               
        
           returnApiData3('0','成功',$data4, $data2,$data3,$count_num);exit;	     
   
     }


     public  function  shop_detail(){
                  $cid = I('cid');;
                  $mid = 3;
                //左侧类别信息
                //此时时间戳
	          $now_time = time(); 
                //当天凌晨
                 $day_break = strtotime(date('Y-m-d'));  
                //本月第一天时间戳
                 $month_first_day = mktime(0,0,0,date('m'),1,date('Y')); 
                //本年第一天时间戳
                 $year_first_day = mktime(0,0,0,1,1,date("Y",time())); 
               
                    $data = M('shop_product')->where('mid="'.$mid.'"')->select();
                    foreach ( $data as $key => $item ){
                       $data[$key]['id'] = $item['id'];
                       $data[$key]['pro_num'] = M('shop_cart')->where('cid = "'.$cid.'" AND pro_id = "'.$data[$key]['id'].'"')->getField('pro_count')?M('shop_cart')->where('cid = "'.$cid.'" AND pro_id = "'.$data[$key]['id'].'"')->getField('pro_count'):0; 
                       $data[$key]['pic_path'] = $item['pic_path']?__VUPLOAD__.'pro/'.$item['pic_path']:""; //产品头像  
                       $data[$key]['month_sale_num'] = M('shop_product_order_pro')->where('pro_id="'.$item['id'].'" AND add_time >="'.$month_first_day .'" AND add_time <="'.$now_time .'"')->count();
                    }
               
               
                   $info = M('shop_product_type')->where('mid="'.$mid.'"')->select();
                   foreach( $info as $key => $item ){
                             $data2[$key]['type_id'] = $item['id'];
                             $data2[$key]['type_name'] = $item['type_name'];
                   }

            
                   $attitude = M('shop_evaluate')->where('mid="'.$mid.'"')->sum('attitude_star'); 
                   $speed = M('shop_evaluate')->where('mid="'.$mid.'"')->sum('speed_star'); 
                   $taste = M('shop_evaluate')->where('mid="'.$mid.'"')->sum('taste_star'); 
                   $evaluate_times = M('shop_evaluate')->where('mid="'.$mid.'"')->count();
                   $comment_star = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));  
                   
                    
                   if(  $comment_star == 1 ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                   }elseif(   $comment_star == 2  ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                   }elseif(   $comment_star == 3  ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                   }elseif(   $comment_star == 4  ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                   }elseif(   $comment_star == 5  ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                   }else{
                       $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                   }
                   
                   
                   $dataInfo =  M('shop')->where('mid="'.$mid.'"')->find();
                   $data3['comment_star_num'] = $comment_star;
                   $data3['shop_name'] = $dataInfo['shop_name'];
                   $data3['shop_intro'] = $dataInfo['shop_intro'];    
                   $data3['address'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('address');
                   $data3['telephone'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('telephone');
                   $data3['class_name'] = M('shop_product_class')->where('id="'.$dataInfo['shop_level_three'].'"')->getField('class_name');
                   $data3['delivery_fee'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('delivery_fee');
                   $data3['min_delivery_fee'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('min_delivery_fee');
                   $data3['shop_heapic'] = $this->md->where("s_id=".$dataInfo['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.$this->md->where("s_id=".$dataInfo['id'])->getField('shop_heapic'):""; //店铺头像
                   $data3['shop_toppics']  =  $this->md->where("s_id=".$dataInfo['id'])->getField('shop_toppics')?__VUPLOAD__.'shop_toppics/'.$this->md->where("s_id=".$dataInfo['id'])->getField('shop_toppics'):"";
                   
                    
                  
                 
                   $cart_ids = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->field('id')->select();
                   //dump($cart_ids);exit;
                   $sum=0;//设变量
                   foreach ( $cart_ids  as $key => $item ){
                        //echo $item['id'];
                        $pro_id = M('shop_cart')->where('id="'.$item['id'].'"')->getField('pro_id');
                        $price = M('shop_product')->where('id="'.$pro_id.'"')->getField('price');
                        $pro_count = M('shop_cart')->where('id="'.$item['id'].'"')->getField('pro_count');
                        $pro_money = $pro_count * $price;
                        $sum += $pro_money;
                        
                    }
                   
                    
                    
                   $pro_num_all = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->sum('pro_count');
                   $data4['pro_num_all'] = $pro_num_all;
                    
                   $data4['min_delivery_fee']=  $data3['min_delivery_fee'];
                   $data4['delivery_fee'] = $data3['delivery_fee'];
                   $data4['pro_money_all'] = $sum;
                   $chajia =  $data3['min_delivery_fee'] - $data4['pro_money_all'];
                   if( $chajia > 0 ){
                       $data4['chajia'] = $chajia;  
                    }else{
                       $data4['chajia'] = "reach"; 
                    }
                  
                
                   returnApiData5('0','成功',$data, $data2,$data3,$data4);exit;	
           }
              
              public function   shop_detail_class(){  //  详情中左下分类筛选
                  $cid = I('cid');
                  $mid  = 3;
                  $typeid = I('type_id');  //上一个接口返给前端的  type_id
                  if( $typeid == ''){
                       $data = M('shop_product')->where('mid="'.$mid.'"')->select(); 
                   }  else {
                       
                       $data = M('shop_product')->where('mid="'.$mid.'" AND type_id="'.$typeid.'"')->select();
                  }
                
                  foreach( $data as $key => $item ){
                       $data[$key]['id'] = $item['id'];
                       $data[$key]['pro_num'] = M('shop_cart')->where('cid = "'.$cid.'" AND pro_id = "'.$data[$key]['id'].'"')->getField('pro_count')?M('shop_cart')->where('cid = "'.$cid.'" AND pro_id = "'.$data[$key]['id'].'"')->getField('pro_count'):0; 
                       $data[$key]['pic_path'] = $item['pic_path']?__VUPLOAD__.'pro/'.$item['pic_path']:""; //产品头像  
                       $data[$key]['month_sale_num'] = M('shop_product_order_pro')->where('pro_id="'.$item['id'].'" AND add_time >="'.$month_first_day .'" AND add_time <="'.$now_time .'"')->count();
                  }
                  
                  
                   
                   
                   $cart_ids = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->field('id')->select();
                   //dump($cart_ids);exit;
                   $sum=0;//设变量
                   foreach ( $cart_ids  as $key => $item ){
                        //echo $item['id'];
                        $pro_id = M('shop_cart')->where('id="'.$item['id'].'"')->getField('pro_id');
                        $price = M('shop_product')->where('id="'.$pro_id.'"')->getField('price');
                        $pro_count = M('shop_cart')->where('id="'.$item['id'].'"')->getField('pro_count');
                        $pro_money = $pro_count * $price;
                        $sum += $pro_money;
                        
                    }
                   
                   $dataInfo =  M('shop')->where('mid="'.$mid.'"')->find();
                   $data3['delivery_fee'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('delivery_fee');
                   $data3['min_delivery_fee'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('min_delivery_fee');
                   
                   $data4['min_delivery_fee'] = $data3['min_delivery_fee'];
                   $data4['delivery_fee'] = $data3['delivery_fee'];
                   $pro_num_all = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->sum('pro_count');
                  // $data4 = $pro_num_all;
                   $data4['pro_num_all'] = $pro_num_all;
                    
                   
                   
                   $data4['pro_money_all'] = $sum;
                   $chajia =  $data3['min_delivery_fee'] - $data4['pro_money_all'];
                   if( $chajia > 0 ){
                       $data4['chajia'] = $chajia;  
                    }else{
                       $data4['chajia'] = "reach"; 
                    }
                    
                    
                returnApiData5('0','成功',$data, $data2,$data3,$data4);exit;	  
              }
     
        public function show_evaluate(){ //商家评论
           $page = I('page');
           $mid = I('mid');  //下方循环评论
           $data = M('shop_evaluate')->where('mid="'.$mid.'"')->order('add_time desc')->select();
           $count_num = M('shop_evaluate')->where('mid="'.$mid.'"')->order('add_time desc')->count();
           foreach (  $data as $key => $item ){
                     
                   $every_star_total = $data[$key]['attitude_star'] + $data[$key]['speed_star'] +$data[$key]['taste_star'];
                   $data[$key]['comment_star'] = round( $every_star_total/3 );
               
                   if( $data[$key]['comment_star'] == 1 ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                   }elseif(  $data[$key]['comment_star'] == 2  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                   }elseif(  $data[$key]['comment_star'] == 3  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                   }elseif(  $data[$key]['comment_star'] == 4  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                   }elseif(  $data[$key]['comment_star'] == 5  ){
                      $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                   }else{
                       $data[$key]['comment_star_value'] = __VUPLOAD__.'evaluate/'."null.png";
                   }
                   
                   $phone_num = M('customer')->where('cid="'.$item['cid'].'"')->getField('phone'); 
                   $data[$key]['phone'] = substr_replace($phone_num,'****',3,4);
                   $data[$key]['heapic'] = M('customer')->where('cid="'.$item['cid'].'"')->getField('heapic')?__VUPLOAD__.'heapic/'.M('customer')->where('cid="'.$item['cid'].'"')->getField('heapic'):""; //用户头像
                   //$data[$key]['evaluate_pics'] = __VUPLOAD__.'evaluate_pics/'.$item['evaluate_pics']; //evaluate_pics
       
                  $evaluate_pics = $item['evaluate_pics']; //evaluate_pics
                  $datapics = explode("|", $evaluate_pics);
                  $data[$key]["evaluate_pics"]=$datapics;
                  foreach ($data[$key]["evaluate_pics"] as $key1 => $value) {
                    $data[$key]["evaluate_pics"][$key1]=__VUPLOAD__.'evaluate_pics/'.$value;
                  }

                    // if(!is_array($evaluate_pics)){
                    //  $datapics = explode("|", $evaluate_pics);
                    //  foreach( $datapics as $key1 => $item1 ){
                    //        $data[$key]['evaluate_array_pic'][$key1]  = __VUPLOAD__.'evaluate_pics/'.$item1;
                    //  }
                     
                    // }
                    
                 }
           
                   // 顶部总的三个评价
                            $attitude_star_total =  M('shop_evaluate')->where('mid="'.$mid.'"')->sum('attitude_star'); 
                              $attitude_star_total_count = M('shop_evaluate')->where('mid="'.$mid.'"')->count();
                                $attitude_star_num = round(   $attitude_star_total / $attitude_star_total_count ); 
                                  if(  $attitude_star_num  == 1 ){
                                     $data1['attitude_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                                   }elseif(   $attitude_star_num  == 2  ){
                                     $data1['attitude_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                                   }elseif(   $attitude_star_num  == 3  ){
                                     $data1['attitude_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                                   }elseif(  $attitude_star_num  == 4  ){
                                     $data1['attitude_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                                   }elseif(   $attitude_star_num  == 5  ){
                                    $data1['attitude_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                                   }else{
                                    $data1['attitude_star_value'] = __VUPLOAD__.'evaluate/'."null.png";
                                   }
                      
                                $speed_star_total =  M('shop_evaluate')->where('mid="'.$mid.'"')->sum('speed_star'); 
                                  $speed_star_total_count = M('shop_evaluate')->where('mid="'.$mid.'"')->count();
                                    $speed_star_num  = round(   $speed_star_total / $speed_star_total_count );
                                   if(  $speed_star_num  == 1 ){
                                     $data1['speed_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                                   }elseif(  $speed_star_num == 2  ){
                                     $data1['speed_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                                   }elseif(   $speed_star_num == 3  ){
                                     $data1['speed_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                                   }elseif(  $speed_star_num == 4  ){
                                     $data1['speed_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                                   }elseif(   $speed_star_num  == 5  ){
                                    $data1['speed_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                                   }else{
                                    $data1['speed_star_value'] = __VUPLOAD__.'evaluate/'."null.png";
                                   } 
                             
                               $taste_star_total =  M('shop_evaluate')->where('mid="'.$mid.'"')->sum('taste_star'); 
                                 $taste_star_total_count = M('shop_evaluate')->where('mid="'.$mid.'"')->count();
                                    $taste_star_num = round(   $taste_star_total / $taste_star_total_count );
                                     if(  $taste_star_num  == 1 ){
                                        $data1['taste_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                                      }elseif(  $taste_star_num == 2  ){
                                        $data1['taste_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                                      }elseif(   $taste_star_num == 3  ){
                                        $data1['taste_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                                      }elseif(  $taste_star_num == 4  ){
                                        $data1['taste_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                                      }elseif(   $taste_star_num  == 5  ){
                                       $data1['taste_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                                      }else{
                                       $data1['taste_star_value'] = __VUPLOAD__.'evaluate/'."null.png";
                                      } 
                               
                       //  print_r($data);exit;
                      //   returnApiData2('0','成功',$data, $data1,$data3);exit;	
                         returnApiData3('0','成功',$data, $data1,$data3,$count_num);exit;	     
           }

           
           
 
	
	
	public function del(){
	        $ids = I('id');
            if($ids){
			if($this->m->where("id=".$ids)->setField('status',0)){   //逻辑删除 
						//****mono删除
						$del['where']=array('id'=>$ids);
						M_G('task')->delete($del);
						//****mono删除
			 returnApiresult("0","成功");exit;
			}else{	
			 returnApiresult("1","失败");exit;
			}
			}else{
			 returnApiresult("1","参数为空");exit;
			}
	}
	
	
	/*
	############################  报名
	*/		
	
	/** 2016-4-27 任务报名
	*
	*/	
	
	
	
	public function add(){
		$uid=$this->userid;   
		$idstop=str_replace('&quot;','',I('id'));
		$idstop=str_replace('"','',$idstop);
		$ids= $idstop?json_decode($idstop,true):"";  //选中通过报名的记录ID
		$task_id=I('task_id'); // 任务ID  [1]
		$databm = $this->m_bm->field('id,skill_id')->where("task_id=".$task_id)->order('id desc')->select();	
		if($databm){
		foreach($databm as $key=>$item){
		if(in_array($item['id'],$ids)){   
		   $this->addok($item['id']);   //跟新报名表的审核状态
		   $info=$this->addorder($uid,$task_id,$item['id'],$item['skill_id']);//审核通过生成单个订单
		   }else{
		   // $this->addel($item['id']); 
		   }}}
		if($info){
		returnApiresult('0','成功');exit;
		}else{
		returnApiresult('1','失败');exit;
		} 
	}
	
		/** 2016-7-6  生成订单 
	*
	*/	
	public function addorder($task_uid,$task_id,$task_item_id,$skill_id){  //生产多个订单
	        $uid=$task_uid;
			$data["id"]=Getmysql_Idstr(4);
		    $data["uid"]=$uid;
			$data["order_no"]=GetorderStr();
			$data["addtime"]= time();
			$infoid = $this->m_order->add($data);
			if($infoid){
			$dataitem["id"]=Getmysql_Idstr(5);
		    $dataitem["task_id"]=(int)$task_id;
			$dataitem["task_item_id"]=(int)$task_item_id;
			$dataitem["task_name"]=M_S('task')->where('id='.$dataitem["task_id"])->getField('in_name');
			$dataitem["order_id"]= $infoid;
			$dataitem["classone"]= M_S('task')->where('id='.$dataitem["task_id"])->getField('classone');
			$dataitem["skill_price"]= M_S('skill')->where('id='.$skill_id)->getField('price');
			$dataitem["skill_unit"]=  M_S('skill')->where('id='.$skill_id)->getField('unit');
			$info = $this->m_order_n->add($dataitem);
			}
			if($info){	
			return $infoid;
			}else{
			return 0;
			}	  
	}

		public function creatOrderNo(){
		$orderNo = date("YmdHis").rand(1000, 9999);
		$isCount = $this->m_order->where(array('order_no'=>$orderNo))->count();
		if($isCount){
		$this->creatOrderNo();
		}
		return $orderNo;
	}
	
	
	
		public function long_pay(){ 
	//	echo 1;exit;
		
		         $order_id=I('order_id');
				 $is_longtime=M_S('order_item')->where("order_id='".$order_id."'")->getField("is_longtime");
				 if($is_longtime==1){  returnApiresult("1","支付时间已经延长过了");exit;     }
				 $auto_paytime=M_S('order_item')->where("order_id='".$order_id."'")->getField("auto_paytime");
				 if($order_id){
				 $auto_paytime=strtotime("+15 day",$auto_paytime?$auto_paytime:time());
				 M_S('order_item')->where("order_id='".$order_id."'")->setField('auto_paytime',$auto_paytime);
	             M_S('order_item')->where("order_id='".$order_id."'")->setField('is_longtime',1);
				 }
				 
			     returnApiresult("0","支付时间延长15天");exit;
		
				
	 } 


}